# -*- coding: utf-8 -*-
import random
import time

from PageClass.boe_MultiDomesticTravelClass.Class_MultiDomesticTravelBoe import Class_MultiDomesticTravelBoe
from TestCases.common import LoginDepend
from TestCases.common._baseClass import _BaseClass
from Util import config, logger, record


class BoeSubmit(_BaseClass):

    def __init__(self, isExit=True):
        self.isExit = isExit
        self.login = LoginDepend('ecsHost', 'user')
        self.MultiDomesticTravel = Class_MultiDomesticTravelBoe(self.login.driver)

    def __exit__(self, *args):
        if super().__exit__(*args):
            if self.isExit:
                self.exit()
        else:
            self.exit()

    def exit(self):
        self.MultiDomesticTravel.driver.quit()

    def submit(self, boe_data):
        # 打开费报
        logger.info(f"费报地址为：{config.urlDict.url.ecsHost}")  # .\Config\test\UrlConfig.yml
        self.MultiDomesticTravel.goToLoginPage(config.urlDict.url.ecsHost)
        time.sleep(random.random())
        # 打开多人差旅报账单
        self.MultiDomesticTravel.open_boe(boe_data.areaType, boe_data.boeType, boe_data.operationType)

        # 获取单号
        boeNo = self.MultiDomesticTravel.getBoeNum()
        record.writeDataToRecord({'boeNo': boeNo})

        # 主表区
        operationType = boe_data.operationType
        self.MultiDomesticTravel.input_operationType(operationType)

        boeAbstract = boe_data.boeAbstract
        self.MultiDomesticTravel.inputboeAbstract(boeAbstract)

        # 选择项目
        Project = boe_data.Project
        self.MultiDomesticTravel.inputProject(Project)

        # 发票明细区,发票明细区给出行人本人新增发票

        # 点击出行人，进入出行人为提单人的新增发票弹框
        bill_owner = boe_data.bill_owner  # 出行人为提单人
        self.MultiDomesticTravel.clickBillOwner(bill_owner)

        # 选择其他纸质票新增
        invoiceData = boe_data.invoiceData[0]
        logger.info(f"发票类型：{invoiceData.invoiceType}，发票参数：{invoiceData}")
        self.MultiDomesticTravel.newInvoice(invoiceData)

        # 为发票选择费用分类
        # 点击费用分类
        self.MultiDomesticTravel.FeeNameClick()

        # 弹框搜索、选择  # mini弹框
        self.MultiDomesticTravel.selectValueFromMiniTable(**{'业务小类编码': boe_data.feeCode, '业务小类名称': boe_data.feeName})

        # 关闭发票选择弹框
        self.MultiDomesticTravel.billwin_close()

        # # 保存单据
        # self.MultiDomesticTravel.click_boeSaveButton()
        # time.sleep(5)
        
        # 提交单据
        message = self.MultiDomesticTravel.click_boeSubmitButton()

        return message
